import 'package:flutter/material.dart';

class CustomSwitch extends StatelessWidget {
  final int value; // 0是关，1是开
  final double size; // 开关的整体大小
  final ValueChanged<int> onChanged; // 状态变化回调
  final Color activeBackgroundColor; // 打开时的背景色
  final Color inactiveBackgroundColor; // 关闭时的背景色

  const CustomSwitch({
    Key? key,
    required this.value,
    required this.size,
    required this.onChanged,
    this.activeBackgroundColor = Colors.green, // 默认绿色
    this.inactiveBackgroundColor = Colors.grey, // 默认灰色
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => onChanged(value == 1 ? 0 : 1),
      child: Container(
        width: size * 2,
        height: size,
        padding: EdgeInsets.all(size * 0.1),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(size),
          color: value == 1 ? activeBackgroundColor : inactiveBackgroundColor,
        ),
        child: AnimatedAlign(
          alignment: value == 1 ? Alignment.centerRight : Alignment.centerLeft,
          duration: const Duration(milliseconds: 200),
          child: Container(
            width: size * 0.8,
            height: size * 0.8,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.white, // 小圆点始终为白色
            ),
          ),
        ),
      ),
    );
  }
}